package edu.neumont.MillerK.Android;

import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.LocationManager;
import android.media.MediaPlayer;
import android.net.*;
import android.net.NetworkInfo.DetailedState;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends Activity {
	private ToggleButton panicToggleButton;
	private ToggleButton screamerToggleButton;
	private ToggleButton wifiToggleButton;
	private ToggleButton bluetoothToggleButton;
	private ToggleButton threeGToggleButton;
	private ToggleButton fourGToggleButton;
	private ToggleButton scannerToggleButton;

	private final int panicModeIndex = 0;
	private final int screamerModeIndex = 1;
	private final int wifiIndex = 2;
	private final int bluetoothIndex = 3;
	private final int threeGIndex = 4;
	private final int fourGIndex = 5;

	private SharedPreferences sharedPrefs;
	private SharedPreferences.Editor prefsEditor;
	private WifiManager wifiManager;
	private BluetoothAdapter bluetoothAdapter;
	private ConnectivityManager connectivityManager;
	private LocationManager locationManager;
	private Context context;
	
	private boolean screamerOn;

	private char[] featureChars;
	

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		context = getApplicationContext();
		connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		locationManager = (LocationManager) context
				.getSystemService(Context.LOCATION_SERVICE);
		wifiManager = (WifiManager) context
				.getSystemService(Context.WIFI_SERVICE);
		bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
		sharedPrefs = getSharedPreferences("default", MODE_PRIVATE);
		prefsEditor = sharedPrefs.edit();
		
		screamerOn = getIntent().getExtras().getBoolean("screamerOn");

		featureChars = sharedPrefs.getString("features", null).toCharArray();

		setupPanicToggleButton();
		setupScreamerToggleButton();
		setupWifiToggleButton();
		setupBluetoothToggleButton();
		setupThreeGToggleButton();
		setupFourGToggleButton();
		
		prefsEditor.putString("features", charArrayToString(featureChars));
		prefsEditor.commit();
		
		setupScannerToggleButton();
	}
	
	private void setupPanicToggleButton(){
		panicToggleButton = (ToggleButton) findViewById(R.id.PanicModeToggle);
		if (!hasGPSDevice(context)
				&& connectivityManager.getAllNetworkInfo() == null) {
			panicToggleButton.setEnabled(false);
			featureChars[panicModeIndex] = '2';
		}
		panicToggleButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				PanicToggleButton();
			}
		});
	}
	
	private void setupScreamerToggleButton(){
		screamerToggleButton = (ToggleButton) findViewById(R.id.ScreamerModeToggle);
		screamerToggleButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				ScreamerToggleButton();
			}
		});
	}
	
	private void setupWifiToggleButton(){
		wifiToggleButton = (ToggleButton) findViewById(R.id.WiFiToggle);
		if (wifiManager == null) {
			wifiToggleButton.setEnabled(false);
			featureChars[wifiIndex] = '2';
		}
		wifiToggleButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				WiFiToggleButton();
			}
		});
	}
	
	private void setupBluetoothToggleButton(){
		bluetoothToggleButton = (ToggleButton) findViewById(R.id.BluetoothToggle);
		if (bluetoothAdapter == null) {
			bluetoothToggleButton.setEnabled(false);
			featureChars[bluetoothIndex] = '2';
		}
		bluetoothToggleButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				BluetoothToggleButton();
			}
		});
	}
	
	private void setupThreeGToggleButton(){
		threeGToggleButton = (ToggleButton) findViewById(R.id.ThreeGToggle);
		if (getNetworkInfo(ConnectivityManager.TYPE_MOBILE) == null
				|| getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
						.getDetailedState() == DetailedState.SUSPENDED) {
			threeGToggleButton.setEnabled(false);
			featureChars[threeGIndex] = '2';
		}
		threeGToggleButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				ThreeGToggleButton();
			}
		});
	}
	
	private void setupFourGToggleButton(){
		fourGToggleButton = (ToggleButton) findViewById(R.id.FourGToggle);
		if (getNetworkInfo(ConnectivityManager.TYPE_WIMAX) == null) {
			fourGToggleButton.setEnabled(false);
			featureChars[fourGIndex] = '2';
		}
		fourGToggleButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				FourGToggleButton();
			}
		});
	}
	
	private void setupScannerToggleButton(){
		scannerToggleButton = (ToggleButton) findViewById(R.id.ScannerToggle);
		scannerToggleButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				ScannerToggleButton();
			}
		});
	}

	private void PanicToggleButton() {

		if (panicToggleButton.getText().equals("off")) {
			setFeature(panicModeIndex, '2');
		} else {
			if (isPanicModeRunning()) {
				setFeature(panicModeIndex, '1');
				Toast.makeText(MainActivity.this, sharedPrefs.getString("features", null), Toast.LENGTH_LONG).show();
			} else {
				setFeature(panicModeIndex, '0');
			}
		}
	}

	private void ScreamerToggleButton() {
		if(screamerToggleButton.getText().equals("off")){
			setFeature(screamerModeIndex, '2');
		}else{
			if(screamerOn){
				setFeature(screamerModeIndex, '1');
			}else{
				setFeature(screamerModeIndex, '0');
			}
		}
	}

	private void WiFiToggleButton() {
		if (wifiToggleButton.getText().equals("off")) {
			setFeature(wifiIndex, '2');
		} else {
			if (wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLED
					|| wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLING) {
				setFeature(wifiIndex, '1');
			} else {
				setFeature(wifiIndex, '0');
			}
		}
	}

	private void BluetoothToggleButton() {
		if (bluetoothToggleButton.getText().equals("off")) {
			setFeature(bluetoothIndex, '2');
		} else {
			if (bluetoothAdapter.isEnabled()) {
				setFeature(bluetoothIndex, '1');
			} else {
				setFeature(bluetoothIndex, '0');
			}
		}
	}

	private void ThreeGToggleButton() {
		if (threeGToggleButton.getText().equals("off")) {
			setFeature(threeGIndex, '2');
		} else {
			TelephonyManager telephonyManager = (TelephonyManager) context
					.getSystemService(Context.TELEPHONY_SERVICE);
			if (telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED
					|| telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTING) {
				setFeature(threeGIndex, '1');
			} else {
				setFeature(threeGIndex, '0');
			}
		}
	}

	private void FourGToggleButton() {
		if (fourGToggleButton.getText().equals("off")) {
			setFeature(fourGIndex, '2');
		} else {
			NetworkInfo networkInfo = getNetworkInfo(ConnectivityManager.TYPE_WIMAX);
			if (networkInfo.getDetailedState() == DetailedState.CONNECTED
					|| networkInfo.getDetailedState() == DetailedState.CONNECTING
					|| networkInfo.getDetailedState() == DetailedState.DISCONNECTED
					|| networkInfo.getDetailedState() == DetailedState.DISCONNECTING) {
				setFeature(fourGIndex, '1');
			} else {
				setFeature(fourGIndex, '0');
			}
		}
	}

	private void ScannerToggleButton() {
		//TO-DO implement scanner mode
	}

	private boolean hasGPSDevice(Context context) {
		if (locationManager == null)
			return false;
		final List<String> providers = locationManager.getAllProviders();
		if (providers == null)
			return false;
		return providers.contains(LocationManager.GPS_PROVIDER);
	}

	private NetworkInfo getNetworkInfo(int networkType) {
		NetworkInfo[] networkInfos = connectivityManager.getAllNetworkInfo();
		for (int count = 0; count < networkInfos.length; count++) {
			if (networkInfos[count].getType() == networkType)
				return networkInfos[count];
		}
		return null;
	}

	private void setFeature(int featureIndex, char featureSetting) {
		featureChars = sharedPrefs.getString("features", null).toCharArray();
		featureChars[featureIndex] = featureSetting;
		prefsEditor.putString("features", charArrayToString(featureChars));
		prefsEditor.commit();
	}

	private boolean isPanicModeRunning() {
		ActivityManager manager = (ActivityManager) getApplicationContext()
				.getSystemService(ACTIVITY_SERVICE);
		for (RunningServiceInfo service : manager
				.getRunningServices(Integer.MAX_VALUE)) {
			if ("edu.neumont.MillerK.Android.LocationService"
					.equals(service.service.getClassName())) {
				return true;
			}
		}
		return false;
	}

	private boolean isMainServiceRunning() {
		ActivityManager manager = (ActivityManager) getApplicationContext()
				.getSystemService(ACTIVITY_SERVICE);
		for (RunningServiceInfo service : manager
				.getRunningServices(Integer.MAX_VALUE)) {
			if ("edu.neumont.MillerK.Android.MainService"
					.equals(service.service.getClassName())) {
				return true;
			}
		}
		return false;
	}

	private String charArrayToString(char[] chars) {
		String charString = "";
		for (char c : chars) {
			charString += c;
		}
		return charString;
	}
}